
package org.itboys.mobile.service;

import org.apache.poi.ss.formula.functions.T;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.excel.ExcelUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.*;
import org.itboys.mobile.entity.mongo.TianJieMember;
import org.itboys.mobile.entity.mysql.TianJieWallet;
import org.itboys.mobile.entity.mysql.TianJieWalletBlock;
import org.itboys.mobile.entity.mysql.TianJieWalletBlockRecord;
import org.itboys.mobile.entity.mysql.TianJieWalletRecord;
import org.itboys.mobile.service.mongo.web.TianJieMemberService;
import org.itboys.mobile.util.DatetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.provider.Sun;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_项目自动任务_权限。
 *
 */

@Service
public class SundayTestService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TianJieWalletMapper walletMapper;
	@Autowired
	private TianJieWalletRecordMapper walletRecordMapper;
	@Autowired
	private TianJieWalletBlockMapper walletBlockMapper;
	@Autowired
	private TianJieWalletBlockRecordMapper walletBlockRecordMapper;




	@Autowired
	private TianJieMemberService memberService;


	@Autowired
	private ResourceHolder resourceHolder;
	/**
	 * 查询买入卖出交易记录，生成excel
	 * @param type 1买入，卖出
	 */
	public void getTradeRecord(Integer type) throws  Exception{
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("type",type);
		List<TianJieWalletBlockRecord> records=walletBlockRecordMapper.getTradeRecord(param);
		List<Long> memberIds = new ArrayList<>();
		for(TianJieWalletBlockRecord record:records){
			memberIds.add(record.getMemberId());
		}
		if(ListUtil.isNotNull(memberIds)){
			param.clear();
			param.put("id in",memberIds);
			List<TianJieMember> members = memberService.list(param);
			for(TianJieWalletBlockRecord walletBlockRecord:records){
				for(TianJieMember member:members){
					if(LongUtil.isNotZreo(walletBlockRecord.getMemberId())&&
							walletBlockRecord.getMemberId().longValue()==member.getId()){
						walletBlockRecord.setMemberName(member.getNickName());
						walletBlockRecord.setMemberMobile(member.getUserName());
						break;
					}

				}
			}
			//生成excel记录
			//excel中对应的标题
			List<String> titlesArray = new ArrayList<String>();
			titlesArray.add("用户ID");
			titlesArray.add("用户昵称");
			titlesArray.add("用户手机号");
			titlesArray.add("交易金额");
			titlesArray.add("交易数量");
			titlesArray.add("交易类型");
			//商品属性中标题对应的字段
			List<String> fieldsArray = new ArrayList<String>();
			fieldsArray.add("memberId");
			fieldsArray.add("memberName");
			fieldsArray.add("memberMobile");
			fieldsArray.add("totalAmount");
			fieldsArray.add("blockNumber");
			fieldsArray.add("type");
			String [] fields = (String [])fieldsArray.toArray((new String[fieldsArray.size()]));
			String [] titles = (String [])titlesArray.toArray((new String[titlesArray.size()]));
			//配置文件输出流
			//System.out.println("文件位置"+outFile.;);
			//设置文件位置
			String fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"-"+new Date().getTime()+".xls";
			//String savePath=fileName;
			String savePath="D:/_projects/sunday/tianjiefinance/static/upload/"+fileName;

			//String imagePath=resourceHolder.getStringValue("imgRoot");
			//String resultPath=File.separator+"excel"+File.separator+fileName;
			OutputStream out = new FileOutputStream(new File(savePath));
			ExcelUtils.exportExcelFile("sheet1", null, titles, fields, records, out);
			logger.info("------------下载路径："+savePath);
		}
	}

	/**
	 * 导出所有账户详情
	 * @throws Exception
	 */
	public void test2() throws  Exception{
		Map<String,Object> param = new HashMap<String,Object>();
		List<TianJieWalletRecord> walletRecords=walletRecordMapper.select(param);
		List<TianJieWalletBlockRecord> walletBlockRecords=walletBlockRecordMapper.select(param);
		param.put("financeIsFinance",1);
		List<TianJieMember> members = memberService.list(param);
		List<Map<String,Object>> results  = new ArrayList<Map<String,Object>>();

		for(TianJieMember member:members){
			Map<String,Object> result = new HashMap<String,Object>();
			result.put("memberId",member.getId());
			result.put("memberName",member.getNickName());
			result.put("memberMobile",member.getUserName());
			Double chargeAmount = 0.00;
			Double rewardAmount =0.00;
			Double cashAmount = 0.00;
			Double cashNumber = 0.00;
			Double buyAmount =0.00;
			Double buyNumber =0.00;
			for(TianJieWalletRecord walletRecord:walletRecords){
				logger.info("记录ID="+walletRecord.getId()+"--memberId="+walletRecord.getMemberId());
				if(LongUtil.isNotZreo(walletRecord.getMemberId())&&
						member.getId()==walletRecord.getMemberId().longValue()) {
					//充值金额
					if (walletRecord.getType() == SundayCommonConstants.wallet_record_type_charge &&
							walletRecord.getStatus() == SundayCommonConstants.YES) {
						chargeAmount += walletRecord.getAmount().doubleValue();
					}
					//提现金额
					if (walletRecord.getType() == SundayCommonConstants.wallet_record_type_cash) {
						if (walletRecord.getAccountStatus() == 1 || walletRecord.getAccountStatus() == 2) {
							cashAmount += walletRecord.getAmount().doubleValue();
							if (walletRecord.getAccountBitcoinNumber() != null) {
								cashNumber += walletRecord.getAccountBitcoinNumber().doubleValue();
							}
						}
					}
					if(walletRecord.getType()==SundayCommonConstants.wallet_record_type_achievement){
						rewardAmount+=walletRecord.getAmount().doubleValue();
					}
				}
			}
			for(TianJieWalletBlockRecord walletBlockRecord:walletBlockRecords){
				if(LongUtil.isNotZreo(walletBlockRecord.getMemberId())&&
						member.getId()==walletBlockRecord.getMemberId().longValue()) {
					//买入金额，买入数量
					if (walletBlockRecord.getType() == SundayCommonConstants.block_record_type_buy) {
						buyAmount += walletBlockRecord.getAmount().doubleValue();
						buyNumber += walletBlockRecord.getBlockNumber().doubleValue();
					}
				}
			}
			result.put("chargeAmount",chargeAmount);
			result.put("rewardAmount",rewardAmount);
			result.put("cashAmount",cashAmount);
			result.put("cashNumber",cashNumber);
			result.put("buyAmount",buyAmount);
			result.put("buyNumber",buyNumber);
			logger.info("用户ID="+member.getId()+"-------"+result);
			results.add(result);
		}

			//生成excel记录
			//excel中对应的标题
			List<String> titlesArray = new ArrayList<String>();
			titlesArray.add("用户ID");
			titlesArray.add("用户昵称");
			titlesArray.add("用户手机号");
			titlesArray.add("充值金额");
			titlesArray.add("业绩奖励金额");
			titlesArray.add("提币（提现）金额");
			titlesArray.add("提币（提现）数量");
			titlesArray.add("区块链买入金额");
			titlesArray.add("区块链买入数量");
			//商品属性中标题对应的字段
			List<String> fieldsArray = new ArrayList<String>();
			fieldsArray.add("memberId");
			fieldsArray.add("memberName");
			fieldsArray.add("memberMobile");
			fieldsArray.add("chargeAmount");
			fieldsArray.add("rewardAmount");

			fieldsArray.add("cashAmount");
			fieldsArray.add("cashNumber");
			fieldsArray.add("buyAmount");
			fieldsArray.add("buyNumber");
			String [] fields = (String [])fieldsArray.toArray((new String[fieldsArray.size()]));
			String [] titles = (String [])titlesArray.toArray((new String[titlesArray.size()]));
			//配置文件输出流
			//System.out.println("文件位置"+outFile.;);
			//设置文件位置
			String fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"-"+new Date().getTime()+".xls";
			//String savePath=fileName;
			String savePath="D:/_projects/sunday/tianjiefinance/static/upload/"+fileName;

			//String imagePath=resourceHolder.getStringValue("imgRoot");
			//String resultPath=File.separator+"excel"+File.separator+fileName;
			OutputStream out = new FileOutputStream(new File(savePath));
			ExcelUtils.exportExcelFile("sheet1", null, titles, fields, results, out);
			logger.info("------------下载路径："+savePath);
		}
}